/*
 * GET home page.
 */

exports.static = function (req, res) {
    var model = GLOBAL.model;
    res.sendfile(model.base_dir+'public'+req.url);
}

exports.index = function (req, res) {
    var page = req.params.page;
    if (!(page > 1))
        page = 1;
    var model = GLOBAL.model;
    var page_size = model.indexPageSize;
    var map_key='/' + page;
    var text_list = model.text_map[map_key];
    var link_list = model.link_map[map_key];
    var attr = {page:page, page_size:page_size, model:model, text_list:text_list, link_list:link_list};
    res.render('index', attr);
};

exports.about = function (req, res) {
    var model = GLOBAL.model;
    var map_key='/about.html';
    var text_list = model.text_map[map_key];
    var link_list = model.link_map[map_key];
    var attr = {page:1, page_size:0, model:model, text_list:text_list, link_list:link_list};
    res.render('about', attr);
};

exports.country = function (req, res) {
    var o = getFileNameAndPage(req.params.country_name);
    var country_name = o.file_name;
    var page = o.page;
    var model = GLOBAL.model;
    var page_size = model.countryPageSize;
    var country = model.getCountry(country_name);
    if (country != null) {
        var map_key= '/' + country.file_name + '/' + page;
        var text_list = model.text_map[map_key];
        var link_list = model.link_map[map_key];
        var attr = {page:page, page_size:page_size, model:model, country:country, text_list:text_list, link_list:link_list};
        res.render('country', attr);
    }
    else {
        response404(res)
    }
}

exports.city = function (req, res) {
    var country_name = req.params.country_name;
    var o = getFileNameAndPage(req.params.city_name);
    var city_name = o.file_name;
    var page = o.page;
    var model = GLOBAL.model;
    var page_size = model.cityPageSize;
    var country = model.getCountry(country_name);
    if (country != null) {
        var city = country.getCity(city_name);
        if (city != null) {
            var map_key = '/' + country.file_name + '/' + city.file_name + '/' + page;
            var text_list = model.text_map[map_key];
            var link_list = model.link_map[map_key];
            var attr = {page:page, page_size:page_size, model:model, country:country, city:city, text_list:text_list, link_list:link_list};
            res.render('city', attr);
        } else {
            response404(res);
        }
    } else {
        response404(res);
    }
}

exports.department = function (req, res) {
    var country_name = req.params.country_name;
    var city_name = req.params.city_name;
    var department_name = req.params.department_name;
    var model = GLOBAL.model;
    var country = model.getCountry(country_name);
    if (country != null) {
        var city = country.getCity(city_name);
        if (city != null) {
            var department = city.getDepartment(department_name);
            if (department != null) {
                var map_key='/' + country.file_name + '/' + city.file_name + '/' + department.file_name;
                var text_list = model.text_map[map_key];
                var link_list = model.link_map[map_key];
                var attr = {page:1, page_size:0, model:model, country:country, city:city, department:department, text_list:text_list, link_list:link_list};
                res.render('department', attr);
            } else {
                response404(res);
            }
        } else {
            response404(res);
        }
    } else {
        response404(res);
    }
}


function getFileNameAndPage(file_name) {
    var page = 1;
    var index = file_name.lastIndexOf('-');
    if (index > 0) {
        page = file_name.substring(index + 1);
        file_name = file_name.substring(0, index);
        if (!(page > 0))
            page = 1;
    }
    return {file_name:file_name, page:page};
}

function response404(res) {
    res.status(404);
    var model = GLOBAL.model;
    var attr = {page:1, page_size:0, model:model, text_list:null, link_list:null};
    res.render('error404', attr);
//    res.error();
}

